<?php

/*
 * Following code will update a product information
 * A product is identified by product id (pid)
 */

// array for JSON response
$response = array();

// check for required fields
if (isset($_POST['id'])) {
    
					$id = $_POST['id'];
					$myArray = json_decode($id, true);

					// include db connect class
					require_once __DIR__ . '/db_connect.php';

					// connecting to db
					$db = new DB_CONNECT();

					
					foreach ($myArray as $key => $value) {

								$get = mysql_query("SELECT id_send,id_receive,is_accept FROM message WHERE id = $value");
								$data = mysql_fetch_array($get);

								$id_send = $data['id_send'];
								$id_receive = $data['id_receive'];
								$is_accept = $data['is_accept'];
								// mysql update row with matched pid
								if($is_accept == 1)
								{
								$result = mysql_query("DELETE FROM message WHERE is_accept = 1 AND id = $value");
								}
								else
								{
								$result = mysql_query("UPDATE message SET is_accept = '1', id_send = '$id_receive', id_receive = '$id_send',id_request ='2'  WHERE id = $value");
								// check if row inserted or not
								if ($result) {
									// successfully updated
									$response["success"] = 1;
									$response["responseMessage"] = "Message successfully updated.";
									//send push notification
										if(mysql_query("SELECT gcm_regid FROM gcm_users WHERE name = '$id_send' limit 1"))
										{
									include_once './GCM.php';
										
									$gcm = new GCM();

										
									$message = array();
										//get message by id
										//get message by id receivece
									$resultMessage  = mysql_query("SELECT id_send,id_receive,is_accept,id_request,name,message.id FROM message,user WHERE id_receive = '$id_receive' AND user_id = '$id_receive' ") or die(mysql_error());
								if (mysql_num_rows($resultMessage) >= 0) {
											// looping through all results
											// game node
											
										$response["message"] = array();
														
										($row = mysql_fetch_array($resultMessage));
															// temp user array
															$message = array();
															$message["id"] = $row["id"];
															$message["id_send"] = $row["id_send"];
															$message["id_receive"] = $row["id_receive"];
															$message["is_acept"] = $row["is_accept"];
															$message["id_request"] = $row["id_request"];
															$message["name"] = $row["name"];
															// push single product into final response array
										
											 //get regId

										$regId = mysql_result(mysql_query("SELECT gcm_regid FROM gcm_users WHERE name = '$id_send' limit 1"),0);
										$registatoin_ids = array($regId);

										 $gcm->send_notification($registatoin_ids, $message);

											   // echo $result;
											
											// echoing JSON response
											echo json_encode($response);
										}
								} else {
									$response["success"] = -1;
									$response["responseMessage"] = "Message not updated.";
									
									// echoing JSON response
									echo json_encode($response);
								}
								}

							}
							}
							}
					 else {
						// required field is missing
						$response["success"] = -1;
						$response["responseMessage"] = "Required field(s) is missing";

						// echoing JSON response
						echo json_encode($response);
					}
?>
